var path="http://localhost:8080/ddln-seek";
var imagePath="http://localhost:8080/ddln-admin/attached/";
$(function(){
    
	//加载父级
    $.ajax({ 
		type:"POST",
		url:path+"/seek/goodsCat/listCatByParentId", 
		data:{parentId:0}, 
		dataType:"json",
		success:function(data){  
			var dom=$(".el-tree-node__children"); 
			dom.html("");
			for (var i = 0; i < data.data.length; i++) {
				
				var s=$('<div class="el-tree-node">'+
																'<div class="el-tree-node__content" style="padding-left: 16px;" goods-cat-id="'+data.data[i].category_id+'"><span class="el-tree-node__expand-icon el-icon-caret-right"></span>'+
																	'<!---->'+
																	'<!----><span class="el-tree-node__label">'+data.data[i].name+'</span></div>'+
																'<!---->'+
															'</div>');
				dom.append(s);
			}
			initcat(); 
		},
		error:function(data){ 
			//alert("对不起，删除失败");
	
		}
	});
    
   
    
   
});

//通过名称获取商品
$("#querybutton").live("click",function(){
	var goodsName=$("#goodsName").val(); 
	var dom=$("#goods-body");
	listGoodsByName(dom,goodsName,1);  
	   
	
});
//点击分类查询商品
$(".el-tree-node__label").live("click",function(){
	var catId=$(this).parent().attr("goods-cat-id");
	var dom=$("#goods-body");
	listGoodsByCat(dom,catId);  
	   
	
});

//分类联动事件
$(".el-icon-caret-right").live("click",function(){
	 $(this).toggle(                
	    		function () {    
	    			var parent_id=$(this).parent().attr("goods-cat-id");
	    			  getCatById(this,parent_id);  
	    		      $(this).addClass("expanded");
	    		},
	    		function () {
	    		       $(this).removeClass("expanded");  
	    		       $(this).parent().next().slideUp(500);  
	    		}
	    );         
	  
	
	 $(this).trigger("click");
});
//获取父级下子分类
function getCatById(obj,parent_id) {

	 $.ajax({ 
			type:"POST",
			url:path+"/seek/goodsCat/listCatByParentId", 
			data:{parentId:parent_id}, 
			dataType:"json",
			success:function(data){  
				var dom=$(obj).parent();    
				var children=$('<div class="el-tree-node__children" style="" data-old-padding-top="" data-old-padding-bottom="" data-old-overflow=""></div>');
				
				for (var i = 0; i < data.data.length; i++) {
					
					var s=$('<div class="el-tree-node">'+
																	'<div class="el-tree-node__content" style="padding-left: 32px;" goods-cat-id="'+data.data[i].category_id+'"><span class="el-tree-node__expand-icon el-icon-caret-right"></span>'+
																		'<!---->'+
																		'<!----><span class="el-tree-node__label">'+data.data[i].name+'</span></div>'+
																	'<!---->'+
																'</div>');
					if(data.data[i].ptype_id!=0){    
						s=$('<div class="el-tree-node">'+
								'<div class="el-tree-node__content" style="padding-left: 48px;" goods-cat-id="'+data.data[i].category_id+'"><span class="el-tree-node__expand-icon el-icon-caret-right"></span>'+
									'<!---->'+
									'<!----><span class="el-tree-node__label">'+data.data[i].name+'</span></div>'+
								'<!---->'+
							'</div>');
					}
					children.append(s);
				}
				dom.next().html("");  
				children.css("display","none");
				dom.after(children);
				children.slideDown(500); 
				
			},
			error:function(data){ 
				//alert("对不起，删除失败");
			}
		});
}
//通过类别获取商品
function listGoodsByCat(obj,cat_id) {

	 $.ajax({ 
			type:"POST",
			url:path+"/seek/goods/listGoodsByCatId", 
			data:{catId:cat_id}, 
			dataType:"json",
			success:function(data){  
				var dom=$(obj);    
				dom.html(""); 
				for (var i = 0; i < data.data.length; i++) {
					
					var s=$('<tr class="el-table__row">'+
					'<td class="el-table_1_column_15 is-center el-table-column--selection">'+
					'<div class="cell"><label role="checkbox" class="el-checkbox"><span aria-checked="mixed" class="el-checkbox__input"><span class="el-checkbox__inner"></span><input class="el-checkbox__original" value="" type="checkbox"></span><!----></label></div>'+
				'</td>'+
				'<td class="el-table_1_column_16 is-center ">'+  
					'<div class="cell"><img data-v-76b0cee4="" src="'+imagePath+data.data[i].goods_img_main+'" class="img-50" style="margin-right: 20px;"></div>'+
				'</td>'+
				'<td class="el-table_1_column_17 is-center ">'+
					'<div class="cell">'+
						'<div data-v-76b0cee4="" class="text-left">'+
							'<a data-v-76b0cee4="" href="http://localhost:8080/sanxing/57.htm" target="_blank" class="link-color">'+data.data[i].name+'</a>'+
						'</div>'+
					'</div>'+
				'</td>'+
				'<td class="el-table_1_column_18 is-center ">'+
					'<div class="cell">'+data.data[i].goods_Cat.name+'</div>'+
				'</td>'+
				'<td class="el-table_1_column_19 is-center ">'+
					'<div class="cell"></div>'+
				'</td>'+
				'<td class="el-table_1_column_20 is-center ">'+
					'<div class="cell">'+data.data[i].price+'</div>'+
				'</td>'+
				'<td class="el-table_1_column_21 is-center ">'+
					'<div class="cell">'+data.data[i].store+'</div>'+    
				'</td>'+
				'<td class="el-table_1_column_22 is-center ">'+
					'<div class="cell">'+
						'<a data-v-76b0cee4="" title="修改" class="t-edit"></a>'+
						'<a data-v-76b0cee4="" title="删除" class="t-del"></a>'+
					'</div>'+
				'</td>'+
			'</tr>');
					
					dom.append(s);
				}
				
			},
			error:function(data){ 
				//alert("对不起，删除失败");
				
			}
		});
}
//通过名称获取商品
function listGoodsByName(obj,goodsName,pageIndex) {

	 $.ajax({ 
			type:"POST",
			url:path+"/seek/goods/listGoodsByName", 
			data:{"goodsName":goodsName,"pageIndex":pageIndex}, 
			dataType:"json",
			success:function(data){  
				var dom=$(obj);    
				dom.html(""); 
				for (var i = 0; i < data.data.rows.length; i++) { 
					
					var s=$('<tr class="el-table__row">'+
					'<td class="el-table_1_column_15 is-center el-table-column--selection">'+
					'<div class="cell"><label role="checkbox" class="el-checkbox"><span aria-checked="mixed" class="el-checkbox__input"><span class="el-checkbox__inner"></span><input class="el-checkbox__original" value="" type="checkbox"></span><!----></label></div>'+
				'</td>'+ 
				'<td class="el-table_1_column_16 is-center ">'+
					'<div class="cell"><img data-v-76b0cee4="" src="'+imagePath+data.data.rows[i].goods_img_main+'" class="img-50" style="margin-right: 20px;"></div>'+
				'</td>'+
				'<td class="el-table_1_column_17 is-center ">'+
					'<div class="cell">'+
						'<div data-v-76b0cee4="" class="text-left">'+
							'<a data-v-76b0cee4="" href="http://localhost:8080/sanxing/57.htm" target="_blank" class="link-color">'+data.data.rows[i].name+'</a>'+
						'</div>'+
					'</div>'+
				'</td>'+
				'<td class="el-table_1_column_18 is-center ">'+
					'<div class="cell">'+data.data.rows[i].goods_Cat.name+'</div>'+
				'</td>'+
				'<td class="el-table_1_column_19 is-center ">'+
					'<div class="cell"></div>'+
				'</td>'+  
				'<td class="el-table_1_column_20 is-center ">'+
					'<div class="cell">'+data.data.rows[i].price+'</div>'+
				'</td>'+
				'<td class="el-table_1_column_21 is-center ">'+
					'<div class="cell">'+data.data.rows[i].store+'</div>'+
				'</td>'+
				'<td class="el-table_1_column_22 is-center ">'+
					'<div class="cell">'+
						'<a data-v-76b0cee4="" title="修改" class="t-edit"></a>'+
						'<a data-v-76b0cee4="" title="删除" class="t-del"></a>'+
					'</div>'+
				'</td>'+
			'</tr>');
					
					dom.append(s);
				}
				
			},
			error:function(data){ 
				//alert("对不起，删除失败");
				
			}
		});
}




